using Admin2025.Application.Services;
using Admin2025.Domain.Entities;
using Admin2025.Domain.Repository;
using Admin2025.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;

namespace Admin2025.Infrastructure.Repositories;

public class RepositoryImpl<T> : RepositoryService<T> where T : EntityBase
{
    private readonly AppDbContext _context;
    private readonly DbSet<T> _dbSet;

    public RepositoryImpl(AppDbContext context)
    {
        _context = context;
        _dbSet = context.Set<T>();
    }

    public override async Task<T> GetByIdAsync(Guid id)
    {
        return await _dbSet.FindAsync(id);
    }

    public override async Task AddAsync(T entity)
    {
        await _dbSet.AddAsync(entity);
        await _context.SaveChangesAsync();
    }

    public override async Task UpdateAsync(T entity)
    {
        _dbSet.Update(entity);
        await _context.SaveChangesAsync();
    }

    public override async Task DeleteAsync(T entity)
    {
        _dbSet.Remove(entity);
        await _context.SaveChangesAsync();
    }
}
